package com.sczfdf.www.sda.gov.cn.modo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.model.AfterExtractor;
import us.codecraft.webmagic.model.HttpRequestBody;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.ExtractBy.Type;
import us.codecraft.webmagic.model.annotation.TargetUrl;
import us.codecraft.webmagic.utils.HttpConstant;

@TargetUrl("[0-9A-Za-z\\.://\\?]+=getBaInfo")
public class GovDO implements AfterExtractor{
	@Override
	public void afterProcess(Page page) {
		for (String json : this.remark) {
			JSONObject pjson = JSONObject.parseObject(json);
			this.remarkString.append(pjson.getString("checkDate") + "对\"");
			this.remarkString.append(pjson.getString("unitName") + "\" 进行了备案后检查；  ");
		}
		if ( !"".equals(this.provinceConfirmKEY) ) {
			for (String json : this.history) {
				JSONObject pjson = JSONObject.parseObject(json);
				this.historyString.append(pjson.getString("operation_date"));
				this.historyString.append("  备案id:" + pjson.getString("processid") + " ");
				this.historyString.append(pjson.getString("remark"));
			}
			this.historyString.append(this.provinceConfirm + "首次备案,首次备案号:" + this.provinceConfirmKEY);
		}else {
			this.historyString.append(this.createTime + "首次备案号:" + this.KEY);
		}
	}
	
	
	public static Request createRequest(String processid) {
		Request request = new Request("http://125.35.6.80:8080/ftba/itownet/fwAction.do?method=getBaInfo");
		
		request.setMethod(HttpConstant.Method.POST);
		request.addHeader("Accept", "*/*");
		request.addHeader("Accept-Encoding", "gzip, deflate");
		request.addHeader("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
		request.addHeader("Cache-Control", "no-cache");
		request.addHeader("Connection", "keep-alive");
		request.addHeader("Content-Type", "application/x-www-form-urlencoded;utf-8");
		request.addHeader("Host", "125.35.6.80:8080");
		request.addHeader("Pragma", "no-cache");
		request.addHeader("Referer", "http://125.35.6.80:8080/ftba/fw.jsp");
//		request.addHeader("User-Agent", UserAgentTool.getUserAgent());
		request.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko");
		request.addHeader("X-Requested-With", "XMLHttpRequest");
		
		Map<String, Object> params = new HashMap<>();
		params.put("processid", processid);
		request.setRequestBody(HttpRequestBody.form(params,"utf-8"));
		
		return request;
	}
	
	@ExtractBy(type=Type.JsonPath,value="$.processid")
	private String KEY;
	@ExtractBy(type=Type.JsonPath,value="$.productname")
	private String name;
	@ExtractBy(type=Type.JsonPath,value="$.provinceConfirm")
	private String createTime;
	@ExtractBy(type=Type.JsonPath,value="$.apply_sn")
	private String recordName;

	
	@ExtractBy(type=Type.JsonPath,value="$.scqyUnitinfo.enterprise_name")
	private String shengchan_companyName;
	@ExtractBy(type=Type.JsonPath,value="$.scqyUnitinfo.enterprise_address")
	private String shengchan_companyAddress;
	
	
	@ExtractBy(type=Type.JsonPath,value="$.sjscqyList[0].enterprise_name")
	private String shiji_companyName;
	@ExtractBy(type=Type.JsonPath,value="$.sjscqyList[0].enterprise_address")
	private String shiji_companyAddress;
	
	@ExtractBy(type=Type.JsonPath,value="$.sjscqyList[0].enterprise_healthpermits")
	private String weiShengXuKe;
	
	@ExtractBy(type=Type.JsonPath,value="$.pfList..cname")
	private List<String> element = new ArrayList<String>();//成分
	private StringBuffer elementString = new StringBuffer();
	@ExtractBy(type=Type.JsonPath,value="$.remark")
	private String explain;//说明
	
	@ExtractBy(type=Type.JsonPath,value="$.ck")
	private List<String> remark = new ArrayList<String>();;//备注
	private StringBuffer remarkString = new StringBuffer();
	@ExtractBy(type=Type.JsonPath,value="$.op")
	private List<String> history = new ArrayList<String>();//历史记录
	private StringBuffer historyString = new StringBuffer();
	
	@ExtractBy(type=Type.JsonPath,value="$.provinceConfirm")
	private String provinceConfirm;//首次备案
	@ExtractBy(type=Type.JsonPath,value="$.pf_processid")
	private String provinceConfirmKEY;//首次备案号
	
	
	public String getProvinceConfirm() {
		return provinceConfirm;
	}
	public void setProvinceConfirm(String provinceConfirm) {
		this.provinceConfirm = provinceConfirm;
	}
	public String getProvinceConfirmKEY() {
		return provinceConfirmKEY;
	}
	public void setProvinceConfirmKEY(String provinceConfirmKEY) {
		this.provinceConfirmKEY = provinceConfirmKEY;
	}
	public String getKEY() {
		return KEY;
	}
	public void setKEY(String kEY) {
		KEY = kEY;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCreateTime() {
		return createTime;
	}
	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}
	public String getRecordName() {
		return recordName;
	}
	public void setRecordName(String recordName) {
		this.recordName = recordName;
	}
	public String getShengchan_companyName() {
		return shengchan_companyName;
	}
	public void setShengchan_companyName(String shengchan_companyName) {
		this.shengchan_companyName = shengchan_companyName;
	}
	public String getShengchan_companyAddress() {
		return shengchan_companyAddress;
	}
	public void setShengchan_companyAddress(String shengchan_companyAddress) {
		this.shengchan_companyAddress = shengchan_companyAddress;
	}
	public String getShiji_companyName() {
		return shiji_companyName;
	}
	public void setShiji_companyName(String shiji_companyName) {
		this.shiji_companyName = shiji_companyName;
	}
	public String getShiji_companyAddress() {
		return shiji_companyAddress;
	}
	public void setShiji_companyAddress(String shiji_companyAddress) {
		this.shiji_companyAddress = shiji_companyAddress;
	}
	public String getWeiShengXuKe() {
		return weiShengXuKe;
	}
	public void setWeiShengXuKe(String weiShengXuKe) {
		this.weiShengXuKe = weiShengXuKe;
	}
	/**
	 * 成分
	 * @return
	 */
	public List<String> getElement() {
		return element;
	}
	/**
	 * 成分
	 * @param element
	 */
	public void setElement(List<String> element) {
		this.element = element;
	}
	/**
	 * 说明
	 * @return
	 */
	public String getExplain() {
		return explain;
	}
	/**
	 * 说明
	 * @param explain
	 */
	public void setExplain(String explain) {
		this.explain = explain;
	}
	/**
	 * 备注
	 * @return
	 */
	public List<String> getRemark() {
		return remark;
	}
	/**
	 * 备注
	 * @param remark
	 */
	public void setRemark(List<String> remark) {
		this.remark = remark;
	}
	public List<String> getHistory() {
		return history;
	}
	public void setHistory(List<String> history) {
		this.history = history;
	}
	public String getRemarkString() {
		return remarkString.toString();
	}
	public void setRemarkString(StringBuffer remarkString) {
		this.remarkString = remarkString;
	}
	public String getHistoryString() {
		return historyString.toString();
	}
	public void setHistoryString(StringBuffer historyString) {
		this.historyString = historyString;
	}
	public String getElementString() {
		for (String string : element) {
			elementString.append(string);
		}
		return elementString.toString();
	}
	public void setElementString(StringBuffer elementString) {
		this.elementString = elementString;
	}
	@Override
	public String toString() {
		return "GovDO [KEY=" + KEY + ", name=" + name + ", createTime=" + createTime + ", recordName=" + recordName
				+ ", shengchan_companyName=" + shengchan_companyName + ", shengchan_companyAddress="
				+ shengchan_companyAddress + ", shiji_companyName=" + shiji_companyName + ", shiji_companyAddress="
				+ shiji_companyAddress + ", weiShengXuKe=" + weiShengXuKe + ", element=" + element + ", explain="
				+ explain + ", remark=" + remark + ", remarkString=" + remarkString + ", history=" + history
				+ ", historyString=" + historyString + ", provinceConfirm=" + provinceConfirm + ", provinceConfirmKEY="
				+ provinceConfirmKEY + "]";
	}
	
}
